<?php


namespace app\api\controller;

use app\api\model\Setting;

/**
 * 发票控制器
 * Class Article
 * @package app\api\controller
 */
class Invoice extends Controller
{
    private $user;

    private $model;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->user = $this->getUser();
        $this->model = new \app\api\model\Invoice();
    }
    public function index(){
        $data = Setting::getItem('store',10001);
        return $this->renderSuccess(html_entity_decode($data['invoice']));
    }
    public function detail($order_id){
        $data = $this->model->where('order_id',$order_id)->with('file')->find();
        if ($data){
            return $this->renderSuccess($data);
        }else{
            return $this->renderError('参数错误，请确认是否已经申请开票？');
        }
    }
    /**
     * 开票信息提交
     * @return array
     */
    public function submit(){
        $data = $this->request->post();
        // 是否已存在
        $exist = $this->model->where('order_id',$data['order_id'])->find();
        if ($exist){
            return $this->renderError('开票信息已存在，请勿重复提交');
        }
        $data['user_id'] = $this->user->user_id;
        $res = $this->model->submit($data);
        return $this->renderSuccess($res);
    }
}